Post

Replies

Boosts

Views

Activity

Keep ScrollView position when adding items on the top
I am using a LayzVStack embedded into a ScrollView. The list items are fetched from a core data store by using a @FetchResult or I tested it also with the new @Query command coming with SwiftData. The list has one hundred items 1, 2, 3, ..., 100. The user scrolled the ScrollView so that items 50, 51, ... 60 are visible on screen. Now new data will be fetched from the server and updates the CoreData or SwiftData model. When I add new items to the end of the list (e.g 101, 102, 103, ...) then the ScrollView is keeping its position. Opposite to this when I add new items to the top (0, -1, -2, -3, ...) then the ScrollView scrolls down. Is there a way with the new SwiftData and SwiftUI ScrollView modifiers to update my list model without scrolling like with UIKit where you can query and set the scroll offset pixel wise?
8
1
9.9k
May ’25
SwiftUI List and LazyVStack becomes jittering with large data
Based on the session video 'Loading and Displaying a Large Data Feed' I observed a performance problem when the number of displayed rows in a List or LazyVStack increases. Instead of an earthquake app lets think of a messaging app. New messages are fetched from a server, injest in a background task into a core data persistent store and displayed with a SwiftUI LazyVStack embedded in a scrollview. The rows in the list have different height. But this is not the problem because I wrote also a test app with a fixed content height for each row. Scrolling through a list of data retrieved by a @FetchRequest works pretty well. I passed the fetched items using a @ObservedObject to the SwiftUI row views so that property faults are only handled for the displayed rows. Row views have also an identifier set so that SwiftUI can reuse them. Everything works fine like in the earthquake example till to the point where the number of fetched items increases (approximate >2000). First when I scroll the list it is smooth but when I scroll more to the bottom items the jittering starts. I observed also that scrolling to the bottom is still smoother than when I scroll back to the begin of the list. Maybe because of the height calculation for the rows above. But this happens only when the number of items fetched from core data is very high. For only a few hundred items the earthquake approach works pretty well in this messaging app environment. Note:I used also request.includesPropertyValues = false as recommended by someone in the internet but this could not fix the jittering with large core data fetch results. Also switching from LazyVStack to List was not a solution for the jitter.
0
0
1.5k
Oct ’22
Code coverage in SwiftUI
Even when you separate business logic code from your SwiftUI views the previews and also view bodies seems both to count for the unit test code coverage. I read many articles in the internet about how to test SwiftUI code and archive good code coverage. But they all seem not to be the perfect solution for this task. Is there any good guide for unit and ui testing SwiftUI code that Apple recommends for their apps?
0
0
1k
Jun ’22
How to deactivate SwiftUI toolbar animation
I have a simple SwiftUI List view with a .toolbar modifier containing three ToolbarItems. The placement is set to bottom. On the iPhone it seems as if the toolbar is animating from the bottom right corner whenever I launch the app. Having this view as a destination view it looks really strange. The NavigationLink causes the List view to appear from the right while the toolbar slides in from the bottom right corner. Is there a way to make the bottom toolbar not to slide it from the corner?
0
0
703
Jun ’22
High battery drain using Xcode 13
I have two Mac Book Pro 16'' 2019. On both I see a very high battery drain using Xcode 13 while using the simulator. Even when the app is not running only the simulator is launched in the background the MacBook gets very hot and the battery drains empty very fast. For the beta this was ok. But with the RC I expected that this was only a temporary problem. Does some else has this problem also?
16
1
6.9k
Mar ’22
How to align slider labels in SwiftUI
On macOS we can add labels directly to SwiftUI slider. Has someone found a way how we can align two or more sliders in a VStack?- label text right aligned- all labels should have the same width (given by the largest text)- all slider bars should have the same but flexible width within the containers frame
4
0
4.1k
Feb ’22
Partial core data updates in Swift
Maybe I misunderstood the the 2019 WWDC video 'Making apps with core data'. Because when I use NSBatchInsertRequest together with NSMergeByPropertyObjectTrumpMergePolicy and unique constraints I can only add new records with the given id. Existing core data objects will not be partially updated. Example Entity: Person Attributes: FirstName = John LastName = Appleseed ID = 1234 (unique constraint) Goal in this example should be to change only the FirstName. As far as I understood the session video we can omit some attributes and call NSBatchInsertRequest with the above ID and change FirstName to 'Johnny'. But this seems not to work. Maybe someone has an idea if I am doing something wrong here or if this is really not possible to omit values and change single attribute values. I want to avoid fetching existing records, updating the values that should be changed and committing the changes back to the persistent store if there is maybe a better solution.
0
0
804
Dec ’21
Updates on SwiftUI list causes detail views to pop
I found this problem many times in the internet but not a solution for this. I have a SwiftUI list with data fetched from a server. The app uses a periodically background sync (into a core data background context). So the rows of this list can change. To make it simple to understand think of a message app like on the iPhone: struct MasterView: View {     @FetchRequest(         sortDescriptors: [NSSortDescriptor(keyPath: \Chat.name, ascending: true)],         animation: .none)     private var chats: FetchedResults<Chat>     var body: some View {         List {             ForEach(chats, id: \.chatID) { chat in                 NavigationLink(destination: DetailView(chat: chat)) {                     Text(chat.name ?? "")                 }             }         }     } } The detail view is not so important here but keeping with the example of a message app you would have something like the following code to display the messages of a selected chat: struct DetailView: View {     let chat: Chat     @FetchRequest(         sortDescriptors: [NSSortDescriptor(keyPath: \Message.timestamp, ascending: true)],         animation: .none)     private var messages: FetchedResults<Message>     var body: some View {         List {             ForEach(messages, id: \.messageID) { message in                 Text(message.messageText ?? "")             }         }         .onAppear {             if let chatID = chat.chatID {                 messages.nsPredicate = NSPredicate(format: "chatID == %@", chatID)             }         }     } } In the internet I saw also other examples without using core data for the master view. If the details view is visible to the user and the master view receives new data from the server the details view will always close (pops) automatically. This happens also when the chatID has not changed and still exists. A change of the master or also if in a split view the master view scrolls outside the visible area then the detail screen will close. As you see I specified the item ids in both ForEach lists and added also in some further tests identifiers to the NavigationLink or destination of the NavigationLink. But as soon the master list updates the old NavigationLink seems to get deallocated and therefore the detail views will close. Does someone know how to fix this? Of course I could add a button inside the masters list, store the selection and place the NavigationLink outside from the list item as I saw in one example, but it seems to be more a quick hack as a good solution.
0
0
742
Oct ’21
Keep ScrollView position when adding items on the top
I am using a LayzVStack embedded into a ScrollView. The list items are fetched from a core data store by using a @FetchResult or I tested it also with the new @Query command coming with SwiftData. The list has one hundred items 1, 2, 3, ..., 100. The user scrolled the ScrollView so that items 50, 51, ... 60 are visible on screen. Now new data will be fetched from the server and updates the CoreData or SwiftData model. When I add new items to the end of the list (e.g 101, 102, 103, ...) then the ScrollView is keeping its position. Opposite to this when I add new items to the top (0, -1, -2, -3, ...) then the ScrollView scrolls down. Is there a way with the new SwiftData and SwiftUI ScrollView modifiers to update my list model without scrolling like with UIKit where you can query and set the scroll offset pixel wise?
Replies
8
Boosts
1
Views
9.9k
Activity
May ’25
SwiftUI List and LazyVStack becomes jittering with large data
Based on the session video 'Loading and Displaying a Large Data Feed' I observed a performance problem when the number of displayed rows in a List or LazyVStack increases. Instead of an earthquake app lets think of a messaging app. New messages are fetched from a server, injest in a background task into a core data persistent store and displayed with a SwiftUI LazyVStack embedded in a scrollview. The rows in the list have different height. But this is not the problem because I wrote also a test app with a fixed content height for each row. Scrolling through a list of data retrieved by a @FetchRequest works pretty well. I passed the fetched items using a @ObservedObject to the SwiftUI row views so that property faults are only handled for the displayed rows. Row views have also an identifier set so that SwiftUI can reuse them. Everything works fine like in the earthquake example till to the point where the number of fetched items increases (approximate >2000). First when I scroll the list it is smooth but when I scroll more to the bottom items the jittering starts. I observed also that scrolling to the bottom is still smoother than when I scroll back to the begin of the list. Maybe because of the height calculation for the rows above. But this happens only when the number of items fetched from core data is very high. For only a few hundred items the earthquake approach works pretty well in this messaging app environment. Note:I used also request.includesPropertyValues = false as recommended by someone in the internet but this could not fix the jittering with large core data fetch results. Also switching from LazyVStack to List was not a solution for the jitter.
Replies
0
Boosts
0
Views
1.5k
Activity
Oct ’22
How to change a macOS menu title in SwiftUI
I am able to insert, delete or replace items in the macOS menu bar using SwiftUIs CommandGroup command. Is it also possible to rename an existing menu title? My goal is to replace the 'file' menu title with 'account'. The rest of the menu bar can stay as it is.
Replies
1
Boosts
1
Views
1.5k
Activity
Sep ’22
Code coverage in SwiftUI
Even when you separate business logic code from your SwiftUI views the previews and also view bodies seems both to count for the unit test code coverage. I read many articles in the internet about how to test SwiftUI code and archive good code coverage. But they all seem not to be the perfect solution for this task. Is there any good guide for unit and ui testing SwiftUI code that Apple recommends for their apps?
Replies
0
Boosts
0
Views
1k
Activity
Jun ’22
How to deactivate SwiftUI toolbar animation
I have a simple SwiftUI List view with a .toolbar modifier containing three ToolbarItems. The placement is set to bottom. On the iPhone it seems as if the toolbar is animating from the bottom right corner whenever I launch the app. Having this view as a destination view it looks really strange. The NavigationLink causes the List view to appear from the right while the toolbar slides in from the bottom right corner. Is there a way to make the bottom toolbar not to slide it from the corner?
Replies
0
Boosts
0
Views
703
Activity
Jun ’22
High battery drain using Xcode 13
I have two Mac Book Pro 16'' 2019. On both I see a very high battery drain using Xcode 13 while using the simulator. Even when the app is not running only the simulator is launched in the background the MacBook gets very hot and the battery drains empty very fast. For the beta this was ok. But with the RC I expected that this was only a temporary problem. Does some else has this problem also?
Replies
16
Boosts
1
Views
6.9k
Activity
Mar ’22
How to align slider labels in SwiftUI
On macOS we can add labels directly to SwiftUI slider. Has someone found a way how we can align two or more sliders in a VStack?- label text right aligned- all labels should have the same width (given by the largest text)- all slider bars should have the same but flexible width within the containers frame
Replies
4
Boosts
0
Views
4.1k
Activity
Feb ’22
HTTP Traffic Instrument using Combine
Is it possible to use the new http traffic instrument with combines dataTaskPublisher? How can I set a label to the task?
Replies
1
Boosts
0
Views
1.5k
Activity
Jan ’22
Partial core data updates in Swift
Maybe I misunderstood the the 2019 WWDC video 'Making apps with core data'. Because when I use NSBatchInsertRequest together with NSMergeByPropertyObjectTrumpMergePolicy and unique constraints I can only add new records with the given id. Existing core data objects will not be partially updated. Example Entity: Person Attributes: FirstName = John LastName = Appleseed ID = 1234 (unique constraint) Goal in this example should be to change only the FirstName. As far as I understood the session video we can omit some attributes and call NSBatchInsertRequest with the above ID and change FirstName to 'Johnny'. But this seems not to work. Maybe someone has an idea if I am doing something wrong here or if this is really not possible to omit values and change single attribute values. I want to avoid fetching existing records, updating the values that should be changed and committing the changes back to the persistent store if there is maybe a better solution.
Replies
0
Boosts
0
Views
804
Activity
Dec ’21
Updates on SwiftUI list causes detail views to pop
I found this problem many times in the internet but not a solution for this. I have a SwiftUI list with data fetched from a server. The app uses a periodically background sync (into a core data background context). So the rows of this list can change. To make it simple to understand think of a message app like on the iPhone: struct MasterView: View {     @FetchRequest(         sortDescriptors: [NSSortDescriptor(keyPath: \Chat.name, ascending: true)],         animation: .none)     private var chats: FetchedResults<Chat>     var body: some View {         List {             ForEach(chats, id: \.chatID) { chat in                 NavigationLink(destination: DetailView(chat: chat)) {                     Text(chat.name ?? "")                 }             }         }     } } The detail view is not so important here but keeping with the example of a message app you would have something like the following code to display the messages of a selected chat: struct DetailView: View {     let chat: Chat     @FetchRequest(         sortDescriptors: [NSSortDescriptor(keyPath: \Message.timestamp, ascending: true)],         animation: .none)     private var messages: FetchedResults<Message>     var body: some View {         List {             ForEach(messages, id: \.messageID) { message in                 Text(message.messageText ?? "")             }         }         .onAppear {             if let chatID = chat.chatID {                 messages.nsPredicate = NSPredicate(format: "chatID == %@", chatID)             }         }     } } In the internet I saw also other examples without using core data for the master view. If the details view is visible to the user and the master view receives new data from the server the details view will always close (pops) automatically. This happens also when the chatID has not changed and still exists. A change of the master or also if in a split view the master view scrolls outside the visible area then the detail screen will close. As you see I specified the item ids in both ForEach lists and added also in some further tests identifiers to the NavigationLink or destination of the NavigationLink. But as soon the master list updates the old NavigationLink seems to get deallocated and therefore the detail views will close. Does someone know how to fix this? Of course I could add a button inside the masters list, store the selection and place the NavigationLink outside from the list item as I saw in one example, but it seems to be more a quick hack as a good solution.
Replies
0
Boosts
0
Views
742
Activity
Oct ’21